<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class News extends Admin_Controller {
	
	function __construct(){
	 	parent::__construct();
	 	$this->admin_navigation->parent_active('news');
	}

	function new_manager(){
	 	$this->load->model('news/news_model');
		$columns = array(
						array(
							'name' => 'ID',
							'type' => 'id',
							'index'=>'new_id',
							'sort_column'=>true
							),
						array(
							'name'=>'Tên',
							'type'=>'text',
							'filter'=>'name',
							'index'=>'name',
							'sort_column'=>true
						),
						array(
							'name'=>'Hot',
							'type'=>'publish',
							'filter'=>'is_hot',
							'index'=>'is_hot',
							'options'=>array(
											 '0'=>'No',
											 '1'=>'Yes'
											)
						),
						array(
							'name'=>__('Category'),
							'type'=>'select',
							'index'=>'category_name',
							'filter'=>'category_id',
							'options'=>$this->news_model->get_categories_options()
						),
						array(
							'name'=>'Ngày tạo',
							'type'=>'date',
							'index'=>'created',
						),
						array(
							'name'=>'Featured',
							'type'=>'publish',
							'filter'=>'featured',
							'index'=>'featured',
							'options'=>array(
											 '0'=>'No',
											 '1'=>'Yes'
											)
						),
						array(
							'name'=>'Trạng thái',
							'type'=>'publish',
							'filter'=>'status',
							'index'=>'status',
							'options'=>array(
											 '0'=>'Tắt',
											 '1'=>'Bật'
											)
						),
		);
		$this->grid->columns($columns);
		$this->grid->datasource('news/news_model','get_news');

		$this->grid->set_title('News Manager');
		$this->grid->add_button('Thêm mới',admin_url('*/news/add_new'),'add');
		$this->grid->set_row_action('edit_new');
		
		$this->grid->total_rows($this->news_model->get_count());
		$this->grid->add_mass_action('delete', array(
			'label'=>'Delete',
			'url'      => admin_url('*/news/delete_new'),
            'confirm'  => 'Are you sure?'
		));
		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function edit_new(){
		$new_id = $this->input->get_param('id',false);
		$this->load->model('news/news_model');
		if ($new_id) {
			$data = $this->news_model->get_new($new_id);
			$this->form->set_values($data);
		}
		$this->form->set_form_attributes(array(
			'id'=>'new_form',
			'action'=>admin_url('*/news/edit_post_new',array('id'=>$new_id)),
			'enctype'=>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array(
			'legend'=>'Ganeral'
		));
		
		$this->form->add_filed('name','text',array(
			'label'=>$this->__('Name'),
			'name'=>'name',
			'required'=>true
		));
		
		$this->form->add_filed('alias','alias',array(
			'label'=>$this->__('Alias'),
			'name'=>'alias',
		));
		
		$this->form->add_filed('cat_id','select',array(
			'label'=>$this->__('Category'),
			'name'=>'category_id',
			'values'=>$this->news_model->get_categories_options()
		));
		$this->form->add_filed('image','image',array(
			'label'=>'Image',
			'name'=>'image',
		));
		$this->form->add_filed('status','select',array(
			'label'=>$this->__('Status'),
			'name'=>'status',
			'values'=>array('Tắt','Bật')
		));
		
		$this->form->add_filed('featured','select',array(
			'label'=>$this->__('Featured'),
			'name'=>'featured',
			'values'=>array('No','Yes')
		));
		
		$this->form->add_filed('is_hot','select',array(
			'label'=>$this->__('Hot'),
			'name'=>'is_hot',
			'values'=>array('No','Yes')
		));
				
		$this->form->add_filed('introtext','textarea',array(
			'label'=>$this->__('Introtext'),
			'name'=>'introtext'
		));
		
		$this->form->add_filed('fulltext','editor',array(
			'label'=>$this->__('Fulltext'),
			'name'=>'fulltext'
		));
		
		$this->load->view('admin/form',array('form'=>$this->form->to_html()));
	}
	
	function add_new(){
	 	$this->_forward('edit_new');
	}
	
	function delete_new(){
		$cids = explode(',',$this->input->get_param('new_id',false));
		$this->load->model('news/news_model');
		foreach ($cids as $cid){
			$this->news_model->delete_new($cid);
		}
		$this->_redirect(admin_url('*/news/new_manager'));
	}
	
	function edit_post_new(){
		$new_id = $this->input->get_param('id',false);
		$this->load->model('news/news_model');
		
		if (($data = $this->input->post())){
			$config['upload_path'] = './images/content/';
			$config['allowed_types'] = 'gif|jpg|png';
//			$config['max_size']	= '100';
//			$config['max_width']  = '1024';
//			$config['max_height']  = '768';
			$this->load->library('upload',$config);
			
			if ($this->upload->do_upload('image')){
				$data['image'] = 'images/content/'.$this->upload->get_file_name();
			}
			if ($new_id){
				$this->news_model->update_new($data,$new_id);
			}else{
				$this->news_model->insert_new($data);
			}
		}
		$this->_redirect(admin_url('*/news/new_manager'));
		
	}
	
	function category_new(){
		$columns = array(
					array(
						'name' => 'ID',
						'type' => 'id',
						'index'=>'category_id',
						'sort_column'=>'desc'
						),
					array(
						'name'=>$this->__('Name'),
						'type'=>'text',
						'filter'=>'name',
						'index'=>'treename',
						'sort_column'=>true
					),
					array(
						'name'=>$this->__('Created'),
						'type'=>'date',
						'index'=>'created',
					),
					array(
						'name'=>$this->__('Status'),
						'type'=>'publish',
						'filter'=>'status',
						'index'=>'status',
						'action'=>'publishcategory',
						'options'=>array(
										 '0'=>'Tắt',
										 '1'=>'Bật'
										)
					),
		);
		$this->grid->columns($columns);
		$this->grid->datasource('news/news_model','get_categories');
		$this->grid->set_title('Category News');
		$this->grid->add_button('Thêm mới',admin_url('*/news/add_category'),'add');
		$this->grid->set_row_action('edit_category');
		$this->grid->add_mass_action('delete',array(
												'label'=>'Delete',
												'url'      => admin_url('*/news/delete_category'),
            									'confirm'  => 'Are you sure?'
											));
		$this->grid->add_mass_action('status',array(
												'label'=>'Status',
												'url'=>admin_url('*/news/change_status_category'),
											));
		$this->load->model('news/news_model');
		$total_rows = $this->news_model->get_count_categories(array(),true);
		$this->grid->total_rows($total_rows);
		
		if ($this->input->is_ajax()){
			$this->output->set_output($this->grid->display());
		}else{
			$this->load->view('admin/grid');
		}
	}
	
	function edit_category(){
		$cat_id = $this->input->get_param('id',false);
		$this->load->model('news/news_model');
		if ($cat_id){
			$this->load->model('categories/categories_model');
			$data = $this->categories_model->get_category($cat_id);
			$this->form->set_values($data);
		}
		$this->form->set_form_attributes(array(
			'id'=>'edit_form',
			'action'=>admin_url('*/news/edit_post_category',array('id'=>$cat_id)),
			'enctype'=>'multipart/form-data'
		));
		$this->form->add_filedset('ganeral',array('legend'=>'Ganeral'));
		
		$this->form->add_filed('name','text',array(
			'label'=>'Name',
			'name'=>'name',
			'required'=>true
		));
		
		$this->form->add_filed('alias','alias',array(
			'label'=>'Alias',
			'name'=>'alias',
			'required'=>false,
		));
		
		$this->form->add_filed('parent_id','select',array(
			'label'=>'Parent',
			'name'=>'parent_id',
			'values'=>$this->news_model->get_categories_options(array('other_category_id'=>$cat_id)),
		));
		$this->form->add_filed('image','image',array(
			'label'=>'Image',
			'name'=>'image',
		));
		$this->form->add_filed('status','select',array(
			'label'=>'Status',
			'name'=>'status',
			'values'=>array('Tắt','Bật')
		));
		$this->form->add_filed('description','editor',array(
			'label'=>'Description',
			'name'=>'description'
		));

		
		$data = array('form_title'=>'Add Category','form'=>$this->form->to_html());
		$this->load->view('admin/form',$data);
	}
	
	function add_category(){
		$this->_forward('edit_category');
	}
	
	function edit_post_category(){
	 	$cat_id = $this->input->get_param('id',false);
	 	$this->load->model('categories/categories_model');
	 	if ($cat_id){
	 		$this->categories_model->update_category($cat_id,$this->input->post());
	 	}else{
			$data = array_merge(array('module'=>'news'),$this->input->post());
			$this->categories_model->insert_category($data);
	 	}
		$this->_redirect(admin_url('*/news/category_new'));
	}
	
	function delete_category(){
		$cid = explode(',', $this->input->get_param('category_id'));
		if (count($cid)>0) {
			foreach ($cid as $id){
				$this->load->model('news/news_model');
				$this->news_model->delete_category($id);
			}
		}
		$this->_redirect(admin_url('*/news/category_new'));
	}
}
